package org.dromara.module.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.module.domain.CnActivity;
import org.dromara.module.service.ICnActivityService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 活动列
 *
 * @author Lion Li
 * @date 2025-04-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/module/activity")
public class CnActivityController extends BaseController {

    private final ICnActivityService cnActivityService;

    /**
     * 查询活动列列表
     */
    @SaCheckPermission("module:activity:list")
    @GetMapping("/list")
    public TableDataInfo<CnActivity> list(CnActivity cnActivity, PageQuery pageQuery) {
        QueryWrapper<CnActivity> qw = new QueryWrapper<>();

        TableDataInfo<CnActivity> td = PageUtils.buildDataInfo(cnActivityService.page(PageUtils.buildPage(),qw));
        List<CnActivity> list = td.getRows();
        return td;
    }

    /**
     * 导出活动列列表
     */
    @SaCheckPermission("module:activity:export")
    @Log(title = "活动列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CnActivity cnActivity, HttpServletResponse response) {
        List<CnActivity> list = cnActivityService.list();
        ExcelUtil.exportExcel(list, "活动列", CnActivity.class, response);
    }

    /**
     * 获取活动列详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("module:activity:query")
    @GetMapping("/{id}")
    public R<CnActivity> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        CnActivity cnActivity = cnActivityService.getById(id);
        if(cnActivity != null){
            if(cnActivity.getStartTime()!= null){
                List<Date> timeRange = new ArrayList<>();
                timeRange.add(cnActivity.getStartTime());
                timeRange.add(cnActivity.getEndTime());
                cnActivity.setTimeRange(timeRange);
            }
        }
        return R.ok(cnActivity);
    }

    /**
     * 新增活动列
     */
    @SaCheckPermission("module:activity:add")
    @Log(title = "活动列", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CnActivity cnActivity) {
        return toAjax(cnActivityService.save(cnActivity));
    }

    /**
     * 修改活动列
     */
    @SaCheckPermission("module:activity:edit")
    @Log(title = "活动列", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CnActivity cnActivity) {
        return toAjax(cnActivityService.updateById(cnActivity));
    }

    /**
     * 删除活动列
     *
     * @param ids 主键串
     */
    @SaCheckPermission("module:activity:remove")
    @Log(title = "活动列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(cnActivityService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
